<?php

/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2022-08-09 15:21:42
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2023-07-27 14:47:46
 */

namespace Common;


use Hyperf\DbConnection\Model\Model;

/**
 * This is the model class for table "{{%diandi_watches_device}}".
 *
 * @property int      $id
 * @property int|null $bloc_id     公司
 * @property int|null $store_id    商户
 * @property int|null $device_id   设备ID
 * @property int|null $member_id   会员ID
 * @property int|null $status      操作状态
 * @property int|null $create_time
 * @property int|null $update_time
 */
class BaseModel extends Model
{
    public static bool $insert = true;
    public static bool $update = true;
    public static bool $delete = true;

    const CREATED_AT  = 'create_time';

    const UPDATED_AT =  'update_time';

    // 验证规则
    public static array $rules = [];

    // 验证错误消息提示
    public static array $messages = [];

    // 验证字段的别名
    public static array $alias = [];



    //新增
    public function addInfo($data): bool|int
    {
        if($this->fill($data, false)->save()){
            $primaryKey = $this->getKeyName();
            return $this->getAttribute($primaryKey);
        }

        return false;  //返回insert_id
    }


    //删除数据
    public function delInfo($where)
    {
        return $this->query()->where($where)->delete();
    }

    //修改数据
    public function updateInfo(array $data, $where): bool
    {
        return $this->query()->where($where)->update($data);
    }

    public function getAll(int $page = 1, int $pageSize = 10, array $where = [], array $orders = []): array
    {
        $query = $this->limit($pageSize * ($page - 1), $pageSize);

        foreach ($orders as $field => $sort) {
            $query->orderBy($field, $sort);
        }
        $list = $query->paginate($pageSize);
        $total = $this->count();

        return ['total' => $total, 'list' => $list];
    }


}
